﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using THSLib;using THSERP.BaseProvider;
using DevExpress.XtraGrid.Views.Grid;
using System.Data;using THSERP.BusinessEntities;
using System.Windows.Forms;

namespace THSERP.Modules.CompanyConstant
{
    public partial class GEBalancesGridControl : THSGridControl
    {
        public override void InitGridControlDataSource()
        {
            base.InitGridControlDataSource();
            CompanyConstantEntities entity = (CompanyConstantEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            DataSet ds = new GEBalancesController().GetAllObjects();
            BindingSource bds = new BindingSource();
            entity.GEBalancesList.Invalidate(ds);
            bds.DataSource = entity.GEBalancesList;
           this.DataSource = bds;
            this.RefreshDataSource();
        }

        protected override GridView InitializeGridView()
        {

            DevExpress.XtraGrid.Views.Grid.GridView gridView = base.InitializeGridView();
            foreach (DevExpress.XtraGrid.Columns.GridColumn column in gridView.Columns)
               column.OptionsColumn.AllowEdit = true;
           gridView.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            //gridView.OptionsBehavior.AllowDeleteRows = DevExpress.Utils.DefaultBoolean.True;
            gridView.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            return gridView;
        }

        protected override void GridView_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            base.GridView_ValidateRow(sender, e);
            DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView)this.MainView;
        }
        protected override void GridView_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            //base.GridView_KeyUp(sender, e);
            GridView gridView = (GridView)sender;
            if (e.KeyCode == Keys.Delete)
            {

                if (gridView.FocusedRowHandle >= 0)
                {
                    GEBalancesInfo objGEBalancesInfo = (GEBalancesInfo)gridView.GetRow(gridView.FocusedRowHandle);
                    if (objGEBalancesInfo != null)
                    {
                                if (objGEBalancesInfo.AAIsLocked)
                                {
                                    MessageBox.Show("Không cho phép xóa số dư đã khóa.", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                                    return;
                                }
                                else
                                {
                                    DialogResult dlr = MessageBox.Show("Bạn chắc chắn xóa số dư đầu kỳ?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                                    if (dlr == DialogResult.Yes)
                                    {
                                        new GEBalancesController().DeleteObject(objGEBalancesInfo.GEBalanceID);
                                        gridView.DeleteRow(gridView.FocusedRowHandle);
                                        gridView.RefreshData();
                                    }
                                    else
                                    {
                                        //InitGridControlDataSource();
                                    }
                                }
                        }
                  

                }
            }
        }
    }
}
